/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <kernel/PLCStringHash.h>

#include <string.h>

using namespace PLSDK;

PLCStringHash::PLCStringHash() : m_count(0), m_hashSize(10) {
	m_table = new Entry*[10];
	for(int i = 0 ; i < m_hashSize ; i++) {
		m_table[i] = 0;
	}
}

PLCStringHash::~PLCStringHash() {
	for(int i = 0 ; i < m_hashSize ; i++) {
		delete m_table[i];
	}
}

void PLCStringHash::put(const char *key, int _value) {
	int index = findKey(key);
	if(index > -1) {
		m_table[index]->value = _value;
	} else {
		addEntry(key, _value);
	}
}

void PLCStringHash::addEntry(const char *key, int _value) {
	int index = getHashValue(key);
	while(m_table[index] != 0) {
		index++;
		if(index >= m_hashSize) {
			rehash();
			addEntry(key, _value);
			return;
		}
	}
	m_table[index] = new Entry;
	m_table[index]->key = key;
	m_table[index]->value = _value;
	m_count++;
}

void PLCStringHash::rehash() {
	Entry **oldEntries = m_table;
	int oldHashSize = m_hashSize;
	m_hashSize += 10;
	m_table = new Entry*[m_hashSize];
	for(int i = 0 ; i < m_hashSize ; i++) {
		m_table[i] = 0;
	}
	int temp = 0;
	for(int index = 0 ; index < oldHashSize; index++) {
		if(oldEntries[index] != 0) {
			temp = getHashValue(oldEntries[index]->key);
			while(m_table[temp] != 0) {
				temp++;
				if(temp >= m_hashSize) {
					for(int i = 0 ; i < oldHashSize ; i++) {
						m_table[i] = oldEntries[i];
					}
					for(int i = oldHashSize - 1 ; i < m_hashSize ; i++) {
						m_table[i] = 0;
					}
					rehash();
					return;
				}
			}
			m_table[temp] = oldEntries[index];
		}
	}
}

int PLCStringHash::getHashValue(const char *key) {
	if(m_count == 0) {
		return -1;
	}
	if(m_count == 1) {
		return 0;
	}
	int _hash = hash(key, strlen(key));
	return _hash % m_hashSize;
}

int PLCStringHash::get(const char *key) {
	int index = findKey(key);
	if(index >= 0 && m_table[index] != 0) {
		return m_table[index]->value;
	}
	return -1;
}

int PLCStringHash::findKey(const char *key) {
	if(m_count == 0) {
		return -2;
	}
	int index = getHashValue(key);
	while(index < m_hashSize) {
		if(m_table[index] != 0) {
			if(strcmp(m_table[index]->key, key) == 0) {
				return index;
			}
		}
		index++;
	}
	return -1;
}

int PLCStringHash::hash(const char *str, int len) {
	int h = 0;
	while (len--) {
		h = (h << 4) + *str++;
		h ^= (h & 0xf0000000) >> 23;
		h &= 0x0fffffff;
	}
	return h;
}
